<?php
/**
 * Manage exchange for user
 *
 * @package    letsManager
 * @subpackage form
 * @author     Jerome Fouilloy
 * @version    SVN: $Rev$
 */
class LetsExchangeUserForm extends BaseLetsExchangeForm
{


  public function setup()
  {
    parent::setup();
    $current_user = sfContext::getInstance()->getUser();
    $current_user_id = $current_user->getUserId();
    $asso_id = $current_user->getAssociationId();
    // get members list enabled and ordered and exclude the current user
    $paramsSearchUser = array(
      'association_id' => $asso_id,
      'state' => MemberTable::STATE_ENABLED,
      'order_by' => 'lastname'
      );
    $memberQuery = LetsMemberTable::getQuerySearch($paramsSearchUser);
    // exclude current user from user list
    $memberQuery->addWhere('m.id != ?', $current_user_id);
    // get parent fields
    $this->useFields(array('name','category_id','amount'));
    // we cannot modify partner member and payment type
    if($this->isNew())
    {
      $this->widgetSchema['other_user_id'] = new letsWidgetFormMemberSelect();
      $this->validatorSchema['other_user_id'] = new sfValidatorDoctrineChoice(array(
        'model' => 'Member',
        'query' => $memberQuery
      ));
      
      $type_choices = LetsExchangeTable::allPaymentTypes();
      $this->widgetSchema['type'] = new sfWidgetFormChoice(array(
        'choices' => $type_choices,
      ));
      $this->validatorSchema['type'] = new sfValidatorChoice(array(
        'choices' => LetsExchangeTable::allPaymentKeys(),
      ));
      $this->widgetSchema['date_exchange'] = new pwWidgetFormJQueryDatePicker(date('Y')-1,date('Y'),false);
      $this->setDefault('date_exchange',time());
      $this->validatorSchema['date_exchange'] = new sfValidatorDate();
      
      $this->widgetSchema['validation_partner'] = new sfWidgetFormInputCheckbox(array(),
        array('class'=>'choice_validation_partner'));
      $this->validatorSchema['validation_partner'] = new sfValidatorBoolean();
      
      $this->widgetSchema['partner_password'] = new sfWidgetFormInputPassword(array(), array('autocomplete'=>'off'));
      $this->validatorSchema['partner_password'] = new sfValidatorString(array('required' => false));
    }

    $this->widgetSchema['category_id']->setOption('query',
      LetsMainCategoryTable::getQueryForUser($current_user)
    );

    $this->validatorSchema['amount']->setOptions(array('min'=>1));

    /*	if(!$this->getObject()->isNew())
     {
     $amount = $this->getObject()->getAmountWithSign($current_user_id);
     // current user is debitor
     if($amount < 0)
     {
     $this->setDefault('type',self::TYPE_PAYMENT);
     }
     else{
     $this->setDefault('type',self::TYPE_REQUEST);
     }
     }*/
    $this->widgetSchema['name']->setAttribute('class', 'formInputLarge');
    // display settings
    $this->widgetSchema['amount']->setAttribute('class', 'formInputShort');
    
    
    $this->widgetSchema->setLabels(array(
        'other_user_id'   => 'Vous avez échangé avec',
        'name'   => 'Le service',
        'category_id'   => 'Dans la catégorie',
        'date_exchange' => "Date de l'échange",
        'amount' => 'Pour un montant de',
        'type' => 'Et vous êtes',
        'partner_password' => 'Mot de passe du partenaire ',
        'validation_partner' => "Votre partenaire est-il/elle à coté pour accepter l'échange ?",

     ));
     $this->widgetSchema->setHelps(array(
        'other_user_id'   => "Saisir les premières lettres du nom ou prénom ou saisir le numéro d'adhérent, puis choisir dans la liste.",
        'name'   => "Service rendu pour l'échange",
        'category_id'   => "Catégorie de l'échange",
        'amount' => "Montant de l'échange",
        'type' => 'Choisir paiement si vous être créditeur'
     ));
  }
}